﻿Chenxin.UI.PointGrid=Chenxin.extend(Chenxin.UI.JsonGrid,{
	renderTo:'div_point_list',
	id:'print_grid',
	scope:this,
	hasPage:false,
	title:'业绩申请-点数分配情况',
	autoLoad:false,
	modelId:'RecordId',
	opstatus:true,
	hasCheckBox:false,
	createTitleTool:function(){					
		return [{
			text: '<span class="x-btn-add"></span>添加分配记录',
			scope:this,
			title:'添加分配记录',
			handler:function(el){
				this.addRecord();
			}
		},{
			text: '<span class="x-btn-edit"></span>编辑分配记录',
			scope:this,
			title:'编辑分配记录',
			handler:function(el){
				this.editRecord();
			}
		},{
			text: '<span class="x-btn-del"></span>删除分配记录',
			scope:this,
			title:'删除分配记录',
			handler:function(el){
				this.delRecord();
			}
		}];
	},
	columns:{
		BuyerOrVenderInfo:{
			head:'客户类型',
			width:60,
			renderer:function(val){
				return val==10?'上家':'下家';
			}
		},
		Depart:{
			head:'门店',
			width:100
		},
		Employee:{
			head:'业务员',
			width:100
		},
		DeparterManagerName:{
			head:'店长',
			width:100
		},
		MangerName:{
			head:'高经',
			width:100
		},
		RegionManagerName:{
			head:'区经',
			width:100
		},
		MajordomoName:{
			head:'总监',
			width:100
		},
		Point:{
			head:'点数',
			width:100
		}
	},
	addRecord:function(){
		if(!this.opstatus){
			return;
		}
	
		if(this.getTotalPoint()==10){
			alert('总点数10已经全部分配，如果需要调整，请先编辑或删除一条记录！');
			return;
		}
		
		this.showPointAddWin();
		this.pointAddWin.index=-1;
		var data={
			Point:10-this.getTotalPoint(),
			BuyerOrVenderInfo:'20'
		};
		
		if(this.getFormInitData){
			this.getFormInitData.call(this.scope,data);
		}
		
		this.pointAddWin.fitData(data);
	},
	getTotalPoint:function(){
		var total=0;
		for(var i=0;i<this.store.rows.length;i++){
			total+=this.store.rows[i].Point;
		}
		return total;
	},
	editRecord:function(){
		if(!this.opstatus){
			return;
		}
		if(!this.currentId){
			alert('请选中一条记录！');
			return;
		}
		this.showPointAddWin();
		var data={};
		Chenxin.apply(data,this.store.rows[this.store.map[this.currentId].index]);
		for(var d in data){
			if(data[d]==''||data[d]=='00000000-0000-0000-0000-000000000000'){
				delete data[d];
			}
		}
		this.pointAddWin.fitData(data);
		this.pointAddWin.index=this.store.map[this.currentId].index;
	},
	showPointAddWin:function(){
		if(!this.pointAddWin){
			this.pointAddWin=new Chenxin.UI.PointAddWin({
				actionUrl:this.actionUrl,	
				scope:this,
				sureAdd:function(){
					var model=this.getData();
					if(!model.Point){
						alert('请填写完必填项！');
						return;
					}
					model.Point=parseFloat(model.Point);
					if((this.scope.getTotalPoint()+model.Point-(this.index==-1?0:this.scope.store.rows[this.index].Point))>10){
						alert('各分配记录的总点数不能超过10');
						return;
					}
					
					if(this.index!=-1){//编辑状态
						model.RecordId = this.scope.store.rows[this.index].RecordId;
						this.scope.store.rows[this.index]=model;
					}else{
						model.RecordId='abc_'+Math.random()*10000000000;
						model.RecordId=model.RecordId.replace('.','');
						this.scope.store.rows.push(model);
						this.scope.store.count++;
					}
					this.scope.reRender();
					this.hide();
				}
			});
		}
		this.pointAddWin.show();
	},
	delRecord:function(){
		if(!this.opstatus){
			return;
		}
		
		if(!this.currentId){
			alert('请选中一条记录！');
			return;
		}
		this.store.rows.splice(this.store.map[this.currentId].index,1);
		this.reRender();
	},
	initComponent:function(){
		Chenxin.UI.PointGrid.superclass.initComponent.call(this);
	}
		
});

Chenxin.UI.PointAddWin=Chenxin.extend(Chenxin.UI.Win,{	
	id:'x_point_add_win',
	width:580,
	hasFoot:true,
	height:200,
	title:'添加点数分配',
	createBtn:function(){
		return [{
			text:'保存',
			handler:this.sureAdd
		},{
			text:'关闭',
			handler:this.hide
		}]	
	},
	fitData:function(model){
		model=model||{};
		curRow = model;
		this.comboList[1].reset();
		this.comboList[2].reset();
		this.comboList[3].reset();
		this.comboList[4].reset();
		this.comboList[5].reset();
		this.comboList[6].reset();
		
		Chenxin.Fn.setFormValue(this.id,this.comboList,model);
		this.resetCombo(model.DepartId,false);
	},
	createForm:function(){
		var html='<table class="tbl_f jqTabWh" style="margin:30px 0px;"><tr>';
		var titleArray=[{
			title:'客户类型',
			id:'BuyerOrVenderInfo',
			type:'div'
		},{
			title:'部门',
			id:'DepartId'
		},{
			title:'业务员',
			id:'EmployeeId'
		},{
			title:'店长',
			id:'DeparterManagerId'
		},{
			title:'高经',
			id:'MangerId'
		},{
			title:'区经',
			id:'RegionManagerId'
		},{
			title:'总监',
			id:'MajordomoId'
		},{
			title:'点数',
			id:'Point',
			name:'Point',
			value:'2'
		}];
		for(var i=0;i<titleArray.length;i++){
			titleArray[i].zIndex=800-i;
			if(i>0&&i%2==0){
				html+='</tr><tr>';
			}
			html+=this.createFormRow(titleArray[i]);
		}
		html+='</tr></table>';
		return html;
	},
	createFormRow:function(opt){
		var cont='';
		if(opt.type=='div'){
			cont='<div id="'+this.id+'_'+opt.id+'" style="z-index: '+opt.zIndex+';margin-right: 2px;" class="SelectWrapSingle"></div>';
		}else{
			cont='<input type="text" id="'+this.id+'_'+opt.id+'" name="'+(opt.name||'')+'" class="inputbox_n fl" style="width:140px" value="'+(opt.value||'')+'"/>';		
		}
		return '<th style="width:80px;">'+opt.title+'：</th><td>'+cont+'</td>';
	},
	getData:function(){
		var _length=0;
		for(var i=0;i<this.comboList.length;i++){
			if(!this.comboList[i].validator()){
				_length++;
			}
		}
		if(_length){
			return {};
		}
		
		return Chenxin.Fn.getFormJson(this.id,this.comboList);
	},
	initCombo:function(){
		var _this=this;
		//客户类型
		var buyerOrVenderInfo = new Chenxin.UI.Combo({
           containerId: '#'+this.id+'_BuyerOrVenderInfo',
           name: 'BuyerOrVenderInfo',
           addPleaseSelected:false,
           tbxWidth: 109,
           items:comboCommon.DealPerson
	   });
	   //业务员
		var employeeId = new Chenxin.UI.RichModelTreeCombo({
            textBoxId:this.id+'_EmployeeId',
            inModelType:'all',
        	txtModelName:'Employee',
            singleSelect:true,
            required:true,
            name:'EmployeeId'
		});
        
		//店长
        var departerManagerId = new Chenxin.UI.RichModelTreeCombo({
            textBoxId:this.id+'_DeparterManagerId',
            inModelType:'all',
        	 txtModelName:'DeparterManagerName',
            singleSelect:true,
            required:true,
            name:'DeparterManagerId'
		});
		//高经
		var mangerId = new Chenxin.UI.RichModelTreeCombo({
            textBoxId:this.id+'_MangerId',
            inModelType:'all',
        	txtModelName:'MangerName',
            singleSelect:true,
            name:'MangerId'
        });
	
		//区经
        var regionMangerId = new Chenxin.UI.RichModelTreeCombo({
            textBoxId:this.id+'_RegionManagerId',
            inModelType:'all',
        	txtModelName:'RegionManagerName',
            singleSelect:true,
            name:'RegionManagerId'
        });

        //总监
        var majordomoId = new Chenxin.UI.RichModelTreeCombo({
            textBoxId:this.id+'_MajordomoId',
            inModelType:'all',
        	txtModelName:'MajordomoName',
            singleSelect:true,
            name:'MajordomoId'
        });
	
		 //门店
        var departId = new Chenxin.UI.DepartTreeCombo({
            textBoxId: this.id+'_DepartId',
            name: 'DepartId',
            inModelType:'all',
            txtModelName:'Depart',
            onSelected:function(node,el){
				if (!node.select) {
					departerManagerId.reset();
					mangerId.reset();
					regionMangerId.reset();
					majordomoId.reset();
					return false;
				}
			
				var _value=this.getValue();
				if( _value != this.defVal ){
					departerManagerId.reset();
					mangerId.reset();
					regionMangerId.reset();
					majordomoId.reset();
				
					_this.resetCombo(_value,true);
				}
           }
        });
	
		this.comboList=[buyerOrVenderInfo,employeeId,departerManagerId,mangerId,regionMangerId,majordomoId,departId];
	},
	resetCombo:function(_value,_flag){
		var employeeId=this.comboList[1];
		var departerManagerId=this.comboList[2];
		var mangerId=this.comboList[3];
		var regionMangerId=this.comboList[4];
		var majordomoId=this.comboList[5];
				
		employeeId.loadUser(_value);
		departerManagerId.loadUser(_value);
		mangerId.loadUser(_value);
		Chenxin.Fn.getModel(_value,this.actionUrl,function(data){
			if(_flag==true && data.DeparterManagerId){		
				departerManagerId.setValue(data.DeparterManagerId,data.DeparterManagerName);
			}
			if(_flag==true && data.MangerId){							
				mangerId.setValue(data.MangerId,data.MangerName);
			}
			if(_flag==true && data.RegionMangerId){							
				regionMangerId.setValue(data.RegionMangerId,data.RegionMangerName);
			}
			if(_flag==true && data.MajordomoId){							
				majordomoId.setValue(data.MajordomoId,data.MajordomoName);
			}
		},this,'getparents');
	},
	initComponent:function(){
		this.content=this.createForm();
		Chenxin.UI.PointAddWin.superclass.initComponent.call(this);
		this.initCombo();
	}
});
